package com.wolf.etl.service.impl;

import com.wolf.etl.model.EtlTaskModel;
import com.wolf.etl.service.IEtlTaskService;
import com.wolf.provider.order.enumerate.QUERY_TYPE;
import com.wolf.provider.order.info.PageBean;
import com.wolf.provider.order.info.PageInfo;
import com.wolf.provider.order.info.ParamBean;
import com.wolf.provider.order.service.impl.BaseServiceImpl;
import com.wolf.util.NumberUtil;
import com.wolf.vo.PageResult;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sdyang
 * @date 2019/10/30 16:20
 **/
@Service("etlTaskServiceImpl")
public class EtlTaskServiceImpl extends BaseServiceImpl<Long> implements IEtlTaskService {

    @Override
    public PageResult findByPage(EtlTaskModel model, PageBean pageBean) {

        StringBuilder sql = new StringBuilder();
        sql.append("select * from etl_task t where 1=1 ");

        List<ParamBean> paramBeans = new ArrayList<ParamBean>();

        if (NumberUtil.isNotEmpty(model.getClient_id())) {
            ParamBean paramBean = new ParamBean();
            paramBean.setWhereField("t.client_id");
            paramBean.setField("client_id");
            paramBean.setValue(model.getClient_id());
            paramBean.setType(QUERY_TYPE.EQUAL.getType());
            paramBeans.add(paramBean);
        }

        PageInfo<EtlTaskModel> vos = findByPageInfo(sql.toString(), paramBeans, model, pageBean);

        PageResult result = new PageResult();
        result.buildResult(vos);
        return result;
    }

    @Override
    public List<EtlTaskModel> findByStatus(String status) {
        StringBuilder sql = new StringBuilder("select * from etl_task where 1=1 ");
        sql.append(" and status='").append(status).append("'");
        Map<String, Object> map = new HashMap<>();

        List<EtlTaskModel> vos = findModelListByMap(sql.toString(), map, new EtlTaskModel());

        return vos;
    }

    @Override
    public List<EtlTaskModel> find(String groupName, String taskName, String status) {
        StringBuilder sql = new StringBuilder("select * from etl_task where 1=1 ");
        sql.append(" and status='").append(status).append("' ");
        sql.append(" and group_name='").append(groupName).append("' ");
        sql.append(" and task_name='").append(taskName).append("' ");
        Map<String, Object> map = new HashMap<>();

        List<EtlTaskModel> vos = findModelListByMap(sql.toString(), map, new EtlTaskModel());

        return vos;
    }

    @Override
    public EtlTaskModel find(Long id, String status) {
        StringBuilder sql = new StringBuilder("select * from etl_task where 1=1 ");
        sql.append(" and status='").append(status).append("' ");
        sql.append(" and id=").append(id);
        Map<String, Object> map = new HashMap<>();

        List<EtlTaskModel> vos = findModelListByMap(sql.toString(), map, new EtlTaskModel());

        if (CollectionUtils.isEmpty(vos)) {
            return null;
        }
        return vos.get(0);
    }


    @Override
    public EtlTaskModel findById(Long id) {
        StringBuilder sql = new StringBuilder("SELECT * from etl_task where 1=1  ");
        sql.append(" and id=").append(id);
        Map<String,Object> map = new HashMap<>();

        List<EtlTaskModel> vos = findModelListByMap(sql.toString(), map, new EtlTaskModel());

        if(vos==null || vos.size()==0){
            return null;
        }
        return vos.get(0);
    }

}
